package com.example.module_main.guide

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.Router
import com.example.lib_common.base.BaseActivity
import com.example.lib_storage.AppMMKV
import com.example.module_main.R
import com.example.module_main.databinding.ActivityGuideBinding
import com.example.module_main.main.MainActivity

/**
 * viewpager2和viewpager的区别：
 * viewpage只支持左右，viewpage2支持左右和上下滑动
 * viewpage2维护recyclerview
 */
@Route(path = Router.MAIN_GUIDE_ACTIVITY)
class  GuideActivity : BaseActivity<GuideViewModel,ActivityGuideBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_guide
    override val mViewModel: GuideViewModel by viewModels()
    //适配器
    private lateinit var mGuidePageAdapter: GuidePageAdapter
    //滑动监听
    private val mCallback:ViewPager2.OnPageChangeCallback by lazy{
        object :ViewPager2.OnPageChangeCallback(){
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                when(position){
                    0 -> {
                        mBinding.guide1.background = getDrawable(R.drawable.bg_indicator_checked)
                        mBinding.guide2.background = getDrawable(R.drawable.bg_indicator_uncheck)
                        mBinding.guide3.background = getDrawable(R.drawable.bg_indicator_uncheck)
                    }
                    1 -> {
                        mBinding.guide2.background = getDrawable(R.drawable.bg_indicator_checked)
                        mBinding.guide1.background = getDrawable(R.drawable.bg_indicator_uncheck)
                        mBinding.guide3.background = getDrawable(R.drawable.bg_indicator_uncheck)
                    }
                    2->{
                        mBinding.guide3.background = getDrawable(R.drawable.bg_indicator_checked)
                        mBinding.guide1.background = getDrawable(R.drawable.bg_indicator_uncheck)
                        mBinding.guide2.background = getDrawable(R.drawable.bg_indicator_uncheck)
                    }
                }

            }

            override fun onPageSelected(position: Int) {
               if(position == 2){
                   hideIndicator()
               }else{
                   showIndicator()
               }
            }
        }
    }

    override fun initView() {
        mGuidePageAdapter = GuidePageAdapter()
        mBinding.vp.adapter = mGuidePageAdapter
        mBinding.vp.offscreenPageLimit = 1
    }

    private fun showIndicator(){
        mBinding.lGuide.visibility = View.VISIBLE
        mBinding.btnEnter.visibility = View.GONE //GONE：隐藏，不占位置  INVISIBLE：隐藏，占位置
    }
    private fun hideIndicator(){
        mBinding.lGuide.visibility = View.GONE
        mBinding.btnEnter.visibility = View.VISIBLE
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        mBinding.vp.unregisterOnPageChangeCallback(mCallback)
    }

    override fun initListener() {
        mBinding.vp.registerOnPageChangeCallback(mCallback)
        mBinding.btnEnter.setOnClickListener {
            AppMMKV.needGuide = false
            ARouter.getInstance().build(Router.MAIN_MAIN_ACTIVITY).navigation()
//            val intent = Intent(this,MainActivity::class.java)
//            startActivity(intent)
            finish()
        }

    }



}